package com.czl.module_service.activity

import android.content.Intent
import android.graphics.drawable.Drawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Html
import android.text.format.DateUtils
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SizeUtils
import com.blankj.utilcode.util.TimeUtils
import com.czl.base.base.BaseActivity
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.HealthReportBean
import com.czl.base.data.bean.tengyun.NoticeListBean
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.databinding.ActivityAddHealthReportBinding
import com.czl.module_service.databinding.ActivityHealthReportDetailBinding
import com.czl.module_service.databinding.ActivityNoticeDetailBinding
import com.czl.module_service.viewmodel.AddHealthReportViewModel
import com.czl.module_service.viewmodel.HealthReportDetailViewModel
import com.github.nikartm.button.util.dpToPx
import com.google.gson.Gson
import java.net.URLDecoder
import java.util.*

@Route(path = AppConstants.Router.Service.F_HEALTH_REPORT_DETAIL)
class HealthReportDetailActivity : BaseActivity<ActivityHealthReportDetailBinding, HealthReportDetailViewModel>() {

    private var bean: Int = 0

    override fun initContentView(): Int {
        return R.layout.activity_health_report_detail
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        viewModel.tvTitle.set("查看健康上报信息")
        bean = intent.getSerializableExtra("data") as Int
        viewModel.selectHealthyAndRelativesById(bean)
    }

    override fun initViewObservable() {
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            // 前一页跳转对象数据
            viewModel.setData(data)
            val viewBinding: ActivityHealthReportDetailBinding = binding
            viewBinding.tvName.text = data.userName
            viewBinding.tvIdCard.text = data.idCard
            viewBinding.tvPhone.text = data.phone
            viewBinding.tvProvince.text = data.province
            viewBinding.tvCity.text = data.city
            viewBinding.tvCommunity.text = data.community
            viewBinding.tvAddress.text = data.address
            if (data.healthyType == 0) {
                viewBinding.tvHealthyType.text = "健康"
                findViewById<LinearLayout>(R.id.ll_visibility1).visibility = View.GONE
                findViewById<LinearLayout>(R.id.ll_visibility2).visibility = View.GONE
                findViewById<LinearLayout>(R.id.ll_visibility3).visibility = View.GONE
            }
            if (data.healthyType == 4) {
                viewBinding.tvHealthyType.text = "已治愈"
                findViewById<LinearLayout>(R.id.ll_visibility1).visibility = View.GONE
                findViewById<LinearLayout>(R.id.ll_visibility2).visibility = View.GONE
                findViewById<LinearLayout>(R.id.ll_visibility3).visibility = View.GONE
            }
            if (data.healthyType == 5) {
                viewBinding.tvHealthyType.text = "其他"
                findViewById<LinearLayout>(R.id.ll_visibility1).visibility = View.GONE
                findViewById<LinearLayout>(R.id.ll_visibility2).visibility = View.GONE
                findViewById<LinearLayout>(R.id.ll_visibility3).visibility = View.GONE
            }
            if (data.healthyType == 1) {
                viewBinding.tvHealthyType.text = "发热"
                findViewById<LinearLayout>(R.id.ll_visibility1).visibility = View.VISIBLE
                findViewById<LinearLayout>(R.id.ll_visibility2).visibility = View.GONE
                findViewById<LinearLayout>(R.id.ll_visibility3).visibility = View.GONE
                if (data.admitted == 1) viewBinding.tvIsShouZhi.text = "是" else viewBinding.tvIsShouZhi.text = "否"
            }
            if (data.healthyType == 2) {
                viewBinding.tvHealthyType.text = "疑似"
                findViewById<LinearLayout>(R.id.ll_visibility1).visibility = View.GONE
                findViewById<LinearLayout>(R.id.ll_visibility2).visibility = View.VISIBLE
                findViewById<LinearLayout>(R.id.ll_visibility3).visibility = View.GONE
                if (data.admitted == 1) viewBinding.tvIsShouZhi2.text = "是" else viewBinding.tvIsShouZhi2.text = "否"
                viewBinding.tvSuspected.text = data.suspected
            }
            if (data.healthyType == 3) {
                viewBinding.tvHealthyType.text = "确诊"
                findViewById<LinearLayout>(R.id.ll_visibility1).visibility = View.GONE
                findViewById<LinearLayout>(R.id.ll_visibility2).visibility = View.GONE
                findViewById<LinearLayout>(R.id.ll_visibility3).visibility = View.VISIBLE
                if (data.admitted == 1) viewBinding.tvIsShouZhi3.text = "是" else viewBinding.tvIsShouZhi3.text = "否"
                if (data.confirmed == 1) viewBinding.tvConfirmed.text = "轻度" else viewBinding.tvConfirmed.text = "重度"
            }
            viewBinding.tvTemp.text = data.temperature + " ℃"
            viewBinding.tvAddress.text = data.address
            if (data.touching == 0) {
                viewBinding.tvTouch.text = "否"
                findViewById<LinearLayout>(R.id.ll_visibility4).visibility = View.GONE
            } else {
                viewBinding.tvTouch.text = "是"
                findViewById<LinearLayout>(R.id.ll_visibility4).visibility = View.VISIBLE
                viewBinding.tvTouchTime.text = TimeUtils.date2String(
                    Date(data.touchingTime.toLong()),
                    "yyyy-MM-dd"
                )
                viewBinding.tvTouchName.text = data.touchingName
                viewBinding.tvTouchPhone.text = data.touchingPhone
            }
            if (data.lifeDifficulties == 0) {
                viewBinding.tvIsDiff.text = "否"
                findViewById<LinearLayout>(R.id.ll_visibility5).visibility = View.GONE
            } else {
                viewBinding.tvIsDiff.text = "是"
                findViewById<LinearLayout>(R.id.ll_visibility5).visibility = View.VISIBLE
                viewBinding.tvDiff.text = data.difficulties
            }
            if (data.healthyRelativesInfoList.size > 0) {
                val linearLayout = viewBinding.llRelation
                for (v in data.healthyRelativesInfoList) {
                    val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,SizeUtils.dp2px(30f))
                    layoutParams.topMargin = dpToPx(5f).toInt()
                    layoutParams.bottomMargin = dpToPx(5f).toInt()
                    val ll = LinearLayout(this)
                    ll.setOnClickListener {
                        val intent = Intent(this, FamilyHealthDetailActivity::class.java)
                        intent.putExtra("data", v)
                        this.startActivity(intent)
                    }
                    ll.setPadding(SizeUtils.dp2px(9f),0,SizeUtils.dp2px(3.5f),0)
                    ll.removeAllViews()
                    ll.orientation = LinearLayout.HORIZONTAL
                    ll.setBackgroundResource(R.drawable.card_shape)
                    ll.gravity = Gravity.CENTER_VERTICAL
                    val tv = TextView(this)
                    val healthyType = if (v.healthyType == 0) "健康" else if (v.healthyType == 1) "发热" else if (v.healthyType == 2) "疑似" else if (v.healthyType == 3) "确诊" else if (v.healthyType == 4) "已治愈" else "其他"
                    tv.text = v.relativesName + "    健康状况   " + healthyType + " 、 体温 " + v.temperature + " ℃"
                    tv.setCompoundDrawables(null,null, ContextCompat.getDrawable(this,R.mipmap.ic_right).apply {
                                                                                                               this!!.setBounds(0,0,this.intrinsicWidth,this.intrinsicHeight)
                    },null)
                    tv.compoundDrawablePadding = SizeUtils.dp2px(9.5f)
                    ll.addView(tv)
                    linearLayout.addView(ll,layoutParams)
                }
            } else {
                val linearLayout = viewBinding.llRelation
                val tv = TextView(this)
                tv.text = "无"
                linearLayout.addView(tv)
            }
        })
    }
}
